package com.cl.spark.nodenew;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cl.spark.base.BaseSparkNode;
import com.cl.spark.dto.SparkParam;
import com.cl.spark.dto.SparkResult;
import com.cl.spark.enums.SparkNodeEnum;
import com.cl.spark.util.OKHttpUtil;
import lombok.Data;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class HttpRequestExecuteNode extends BaseSparkNode {

    private final static Map<String, RequestExtra> requestExtraMap = new ConcurrentHashMap<>();

    @Override
    public SparkResult process(SparkParam sparkParam) {
        JSONArray res = new JSONArray();
        JSONObject expressionObj = sparkParam.getNodeExpression();
        String url = expressionObj.getString("url");
        int requestMaxCount = expressionObj.getIntValue("requestMaxCount");
        long interval = expressionObj.getLongValue("interval");
        if (requestExtraMap.containsKey(url)) {
            RequestExtra requestExtra = requestExtraMap.get(url);
            requestExtra.setInterval(interval);
            requestExtra.setRequestMaxCount(requestMaxCount);

            long lastRequestTime = requestExtra.getLastRequestTime();

            Instant lastRequestTimeInstant = Instant.ofEpochMilli(lastRequestTime);
            LocalDateTime lastRequestTimeDate = lastRequestTimeInstant.atZone(ZoneId.systemDefault()).toLocalDateTime();
            LocalDateTime now = LocalDateTime.now();
            long nowTime = new Date().getTime();

            // 计算是否已隔天
            LocalDate localDate = lastRequestTimeDate.toLocalDate();
            LocalDate localDate1 = now.toLocalDate();
            if (localDate.compareTo(localDate1) != 0) {
                requestExtra.setRequestCount(0);
            }

            // 计算访问次数是否超过
            if (requestExtra.requestCount >= requestExtra.requestMaxCount) {
                return SparkResult.success(res);
            } else {
                requestExtra.setRequestCount(requestExtra.getRequestCount() + 1);
            }

            // 计算间隔时间
            if (nowTime - lastRequestTime < requestExtra.getInterval()) {
                try {
                    requestExtra.setLastRequestTime(nowTime + requestExtra.getInterval() - (nowTime - lastRequestTime));
                    Thread.sleep(requestExtra.getInterval() - (nowTime - lastRequestTime));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                requestExtra.setLastRequestTime(nowTime);
            }


        } else {
            RequestExtra requestExtra = new RequestExtra();
            requestExtra.setRequestCount(1);
            requestExtra.setLastRequestTime(new Date().getTime());
            requestExtraMap.put(url, requestExtra);
        }
        String method = expressionObj.getString("method");
        String param = expressionObj.getString("param");

        if ("POST".equals(method)) {
            OKHttpUtil.okhttpPostString(url, JSONObject.parseObject(param));
        } else if ("GET".equals(method)) {
            OKHttpUtil.okhttpGetString(url, JSONObject.parseObject(param));
        }


        return SparkResult.success(res);
    }

    @Override
    public SparkNodeEnum getType() {
        return SparkNodeEnum.HTTP_REQUEST_EXECUTE;
    }

    @Data
    static class RequestExtra {
        private Integer requestCount = 0;
        private Long lastRequestTime;
        private Integer requestMaxCount = 0;
        private Long interval;

    }
}
